package com.news.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.news.model.common.dtos.PageResponseResult;
import com.news.model.common.dtos.ResponseResult;
import com.news.model.common.enums.AppHttpCodeEnum;
import com.news.model.user.dtos.ApUserRealnameDto;
import com.news.model.user.pojos.ApUserRealname;
import com.news.user.mapper.ApUserRealnameMapper;
import com.news.user.service.ApUserRealnameService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class ApUserRealnameServiceImp extends ServiceImpl<ApUserRealnameMapper, ApUserRealname>
        implements ApUserRealnameService {

    /**
     * 用户审核列表
     * @param apUserRealnameDto
     * @return
     */
    @Override
    public ResponseResult pageList(ApUserRealnameDto apUserRealnameDto) {

        if (apUserRealnameDto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        IPage page = new Page(apUserRealnameDto.getPage(), apUserRealnameDto.getSize());
        //status条件判断
        LambdaQueryWrapper<ApUserRealname> eq = new LambdaQueryWrapper<>();
        if (apUserRealnameDto.getStatus() != null) {
            eq = Wrappers.<ApUserRealname>lambdaQuery()
                    .eq(ApUserRealname::getStatus, apUserRealnameDto.getStatus());
        }
        eq.orderByDesc(ApUserRealname::getCreatedTime);

        page = page(page, eq);

        ResponseResult responseResult = new PageResponseResult(apUserRealnameDto.getPage()
                , apUserRealnameDto.getSize(), (int) page.getTotal());
        responseResult.setData(page.getRecords());
        return responseResult;
    }

    /**
     * 审核通过
     * @param apUserRealnameDto
     * @return
     */
    @Override
    public ResponseResult authPass(ApUserRealnameDto apUserRealnameDto) {

        if (apUserRealnameDto == null){
            return ResponseResult.okResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        ApUserRealname apUserRealname = new ApUserRealname();
        apUserRealname.setId(apUserRealnameDto.getId());
        apUserRealname.setStatus((short) 9);
        updateById(apUserRealname);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 审核失败
     * @param apUserRealnameDto
     * @return
     */
    @Override
    public ResponseResult authFail(ApUserRealnameDto apUserRealnameDto) {

        if (apUserRealnameDto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        ApUserRealname apUserRealname = new ApUserRealname();
        apUserRealname.setId(apUserRealnameDto.getId());
        apUserRealname.setStatus((short) 2);
        apUserRealname.setReason(apUserRealnameDto.getMsg());
        updateById(apUserRealname);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
